// 推荐模块工具函数

import { 
  TaskStatus, 
  RecommendationContentType, 
  TaskStatusStatistics,
  ContentFeatureDimensions 
} from '../types/recommendation/recommendation';

/**
 * 获取任务状态的中文描述
 */
export const getTaskStatusText = (status: TaskStatus): string => {
  const statusMap = {
    [TaskStatus.PENDING]: '待处理',
    [TaskStatus.PROCESSING]: '处理中',
    [TaskStatus.COMPLETED]: '已完成',
    [TaskStatus.FAILED]: '失败'
  };
  return statusMap[status] || '未知状态';
};

/**
 * 获取推荐内容类型的中文描述
 */
export const getRecommendationContentTypeText = (type: RecommendationContentType): string => {
  const typeMap = {
    [RecommendationContentType.IMAGE]: '图片',
    [RecommendationContentType.VIDEO]: '视频'
  };
  return typeMap[type] || '未知类型';
};

/**
 * 计算任务状态统计的总数
 */
export const getTotalTaskCount = (statistics: TaskStatusStatistics): number => {
  return Object.values(statistics).reduce((sum, count) => sum + count, 0);
};

/**
 * 计算任务完成率
 */
export const getTaskCompletionRate = (statistics: TaskStatusStatistics): number => {
  const total = getTotalTaskCount(statistics);
  if (total === 0) return 0;
  return Math.round((statistics[TaskStatus.COMPLETED] / total) * 100);
};

/**
 * 计算任务失败率
 */
export const getTaskFailureRate = (statistics: TaskStatusStatistics): number => {
  const total = getTotalTaskCount(statistics);
  if (total === 0) return 0;
  return Math.round((statistics[TaskStatus.FAILED] / total) * 100);
};

/**
 * 判断是否有失败任务
 */
export const hasFailedTasks = (statistics: TaskStatusStatistics): boolean => {
  return statistics[TaskStatus.FAILED] > 0;
};

/**
 * 判断是否有处理中的任务
 */
export const hasProcessingTasks = (statistics: TaskStatusStatistics): boolean => {
  return statistics[TaskStatus.PROCESSING] > 0;
};

/**
 * 格式化数字显示（添加千分位分隔符）
 */
export const formatNumber = (num: number): string => {
  return num.toLocaleString('zh-CN');
};

/**
 * 格式化时间间隔显示
 */
export const formatTimeInterval = (minutes: number): string => {
  if (minutes < 60) {
    return `${minutes}分钟`;
  } else if (minutes < 1440) {
    const hours = Math.floor(minutes / 60);
    const remainingMinutes = minutes % 60;
    return remainingMinutes > 0 ? `${hours}小时${remainingMinutes}分钟` : `${hours}小时`;
  } else {
    const days = Math.floor(minutes / 1440);
    const remainingHours = Math.floor((minutes % 1440) / 60);
    return remainingHours > 0 ? `${days}天${remainingHours}小时` : `${days}天`;
  }
};

/**
 * 获取内容特征维度的中文描述
 */
export const getFeatureDimensionText = (dimension: keyof ContentFeatureDimensions): string => {
  const dimensionMap = {
    dim1: '具象vs抽象',
    dim2: '理性vs情感',
    dim3: '系统vs探索',
    dim4: '个人vs社群',
    dim5: '专业vs通俗',
    dim6: '经典vs新潮',
    dim7: '实用vs娱乐',
    dim8: '极简vs丰富'
  };
  return dimensionMap[dimension] || '未知维度';
};

/**
 * 获取特征维度值的描述
 */
export const getFeatureDimensionValueText = (
  dimension: keyof ContentFeatureDimensions, 
  value: number
): string => {
  const descriptions = {
    dim1: value < 0.5 ? '偏具象' : '偏抽象',
    dim2: value < 0.5 ? '偏理性' : '偏情感',
    dim3: value < 0.5 ? '偏系统' : '偏探索',
    dim4: value < 0.5 ? '偏个人' : '偏社群',
    dim5: value < 0.5 ? '偏专业' : '偏通俗',
    dim6: value < 0.5 ? '偏经典' : '偏新潮',
    dim7: value < 0.5 ? '偏实用' : '偏娱乐',
    dim8: value < 0.5 ? '偏极简' : '偏丰富'
  };
  return descriptions[dimension] || '未知';
};

/**
 * 验证特征维度值是否有效
 */
export const isValidDimensionValue = (value: number): boolean => {
  return value >= 0 && value <= 1;
};

/**
 * 格式化特征维度值显示
 */
export const formatDimensionValue = (value: number): string => {
  return value.toFixed(2);
}; 